<template>
  <div class="container">
    <div class="header">
      <div>
        <span class="title">用户昵称：</span>
        <el-input v-model="search.name" placeholder="请输入内容" size="medium" />
      </div>

      <div>
        <span class="title">联系电话：</span>
        <el-input v-model="search.phone" placeholder="请输入内容" size="medium" />
      </div>

      <div>
        <span class="title">注册时间：</span>
        <el-date-picker
          v-model="timer"
          size="medium"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
          :picker-options="pickerOptions"
          @change="selectTime"
        />
      </div>

      <div>
        <el-button size="small" type="primary" @click="reset">重置</el-button>
        <el-button size="small" type="primary" @click="getList">搜索</el-button>
      </div>
    </div>

    <div class="content">
      <el-table
        v-loading="loading"
        :data="list"
        :header-cell-style="{'color':'#000'}"
        style="width: 100%"
      >
        <el-table-column
          prop="id"
          label="ID"
          width="80"
          align="center"
        />
        <el-table-column
          prop="nickname"
          label="昵称"
          align="center"
          width="140"
        >
          <template slot-scope="scope">
            {{ scope.row.nickname || '—' }}
          </template>
        </el-table-column>
        <el-table-column
          label="性别"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.gender==1">男</span>
            <span v-if="scope.row.gender==2">女</span>
            <span v-if="scope.row.gender==0">未知</span>
          </template>
        </el-table-column>
        <el-table-column
          label="手机号"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            {{ scope.row.phone || '—' }}
          </template>
        </el-table-column>
        <el-table-column
          label="头像"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <img :src="scope.row.avatar" alt="" style="width:60px;height:60px;object-fit: cover;border-radius: 10px;">
          </template>
        </el-table-column>
        <el-table-column
          label="所在地区"
          align="center"
          width="180"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.province">{{ scope.row.province }} {{ scope.row.city }}</span>
            <span v-else>—</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="order_count"
          label="成交单量（单）"
          align="center"
          width="180"
        />
        <el-table-column
          prop="order_total_money"
          label="成交金额（元）"
          align="center"
          width="180"
        />
        <el-table-column
          prop="balance"
          label="账户余额（元）"
          align="center"
          width="180"
        />
        <el-table-column
          label="注册时间"
          align="center"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.updated_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <div class="btn">
              <span @click="changeStatus(scope.$index, scope.row)">{{ scope.row.status == 1 ? '禁用' : '启用' }}</span>
            </div>
            <div class="btn">
              <span @click="rechargeSubmit(scope.row)">充值</span>
            </div>

            <div class="btn">
              <span @click="delUser(scope.row)">删除</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="pagesize"
          :page-sizes="[7, 10, 20, 30]"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
<el-dialog  title="会员充值" :visible.sync="dialogVisible" width="40%" :modal="false">
              <el-form ref="form" :model="rechargeform" label-width="80px">

                <el-form-item label="充值类型">
                  <el-radio-group  v-model="rechargeform.flag" >
                    <el-radio :label="1" >余额增加</el-radio>
                    <el-radio :label="2" >余额减少</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="充值数值">
                  <el-input v-model="rechargeform.integral" type='number'></el-input>
                </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="rechargeSubmitOk()">确 定</el-button>
              </span>
            </el-dialog>
  </div>

</template>

<script>

export default {
  inject: ['reload'],
  data() {
    return {
      rechargeuserid:0,
      dialogVisible:false,
      rechargeform: {
        flag:1,
        integral:''
      },
      loading: false,
      timer: '',
      search: {
        phone: '',
        name: '',
        start_time: '',
        end_time: ''
      },

      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },

      list: [],
      total: 0,
      pagesize: 7, // 默认一页6条
      currentPage: 1 // 当前页
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 重置
    reset() {
      this.currentPage = 1
      this.pagesize = 6
      this.search.phone = ''
      this.search.name = ''
      this.search.start_time = ''
      this.search.end_time = ''
      this.timer = ''
      this.list = []
      this.getList()
    },

    // 获取表格数据
    getList() {
      this.loading = true
      this.$request.get('/admin/customer', {
        params: {
          ...this.search,
          limit: this.pagesize,
          page: this.currentPage
        }
      }).then(res => {
        this.loading = false
        this.list = res.data.data
        this.total = res.data.total
      }).catch(() => {
        this.loading = false
      })
    },

    // 设置每页条数
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // 分页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 选择时间
    selectTime(e) {
      this.search.start_time = e[0]
      this.search.end_time = e[1]
    },

    // 修改状态
    changeStatus(idx, e) {     
      this.$request.post(`/admin/customer/status/${e.id}`, { status: e.status == 1 ? 0 : 1 }).then(res => {
      }).catch()
    },
    rechargeSubmitOk(){
      var id = this.rechargeuserid;   
      this.$request.post(`/admin/customer/recharge/${id}`, {flag: this.rechargeform.flag,integral:this.rechargeform.integral}).then(res => {
          if (res.code == 200) {
          	this.$util.eltips(res.msg, 'success')
            this.dialogVisible = false
            this.getList()
          } else {
          	this.$util.eltips(res.msg, 'error')
          }
      }).catch()
    },
    //会员充值
    rechargeSubmit(e) {
      console.log("ID:",e.id);
      this.dialogVisible = true;
      this.rechargeuserid = e.id;
    },
    // 删除会员
    delUser(e) {
      this.$confirm("此操作将删除该会员其关联的数据, 是否继续?", "提示", {
      		confirmButtonText: "确定",
      		cancelButtonText: "取消",
      		type: "warning",
      	})
      	.then(() => {
      		this.$request.post(`/admin/customer/deluser/${e.id}`).then(res => {
            if (res.code == 200) {
            	this.$util.eltips(res.msg, 'success')
              this.getList()
            } else {
            	this.$util.eltips(res.msg, 'error')
            }
      		}).catch()
      	})
      	.catch(() => {});


    }
  }
}
</script>

<style lang="scss" scoped>
  .header{
    width: 100%;
    height: auto;
    padding: 10px;
    background-color: #fff;
    display: flex;
    justify-content: space-between;

    &>div{
      width: calc(100% / 4 - 20px);
      display: flex;
      align-items: center;

      &:nth-child(3){
        width: calc(100% / 4 + 60px);
      }
      &:nth-child(4){
        width: calc(100% / 4 - 100px);
      }
      &>.title{
        display: inline-block;
        width: 100px;
        font-size: 14px;
      }
    }
  }
  .content{
    width: 100%;
    margin-top: 10px;
  }
  .pagination{
    width: 100%;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .btn{
    &>span{
      display: inline-block;
      color: #1989fa;
      width: 40px;
      text-align: center;
      cursor: pointer;
    }
  }

</style>
